home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1374 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Mem Allocation in functions
  5. Date: Sat, 13 Jan 1996 06:34:24 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4d7js3$hql@fountain.mindlink.net>
  8. References: <00001a80+00006ba7@msn.com>
  9. NNTP-Posting-Host: line205.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Gordon_B@msn.com (Gordon Thomas) wrote:
  13.  
  14. >I should know the answer, but I don't. I have a function
  15.  
  16. >#include <usual stuff>
  17. > void foo(float *stuff, int *length)
  18. >   {
  19. >    int i;
  20. >    stuff = calloc(*length, sizeof(float));
  21. >//    diddle around and put numbers in stuff[i]
  22. >    return;
  23. >   }
  24. >//
  25. > int main(void)
  26. >  {
  27. >   int count;
  28. >   float *somenums;
  29. >   int np = 20;
  30. >   void foo(float *, int *);
  31. >   foo(somenums,&np);
  32. >    for(count = 0 ; count < np ; count++)
  33. >      fprintf(stdout,"%4d %10.2f\n",count, somenums[count] ;
  34. >   free(somenums);
  35. >   return 0;
  36. >  }
  37. >My question is: in foo, memory was allocated to the pointer "somenums" and
  38. >at the return the array was filled with the correct values.  However,
  39. >in main the array is junk NANs, and in fact the pointer is pointing into DS.
  40. >On the other hand, if "somenums" is calloc'd in main, correct values are
  41. >obtained.
  42. >What language spec am I ignorant of that vitiates the above scheme.
  43. >Thanx
  44. >Gordon
  45.  
  46.      C uses call by value.  In order to modify a pointer, you must
  47. pass a pointer to it i.e. pointer to pointer to whatever.
  48.  
  49. Sincerely,
  50.  
  51. Gene Wirchenko
  52.  
  53. C Pronunciation Guide:
  54.      y=x++;     "wye equals ex plus plus semicolon"
  55.      x=x++;     "ex equals ex doublecross semicolon"
  56.  
  57.